﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

class Player : MonoBehaviour
{
    Transform m_CacheTrans;
    public Transform CacheTrans
    {
        get
        {
            if (m_CacheTrans == null)
            {
                m_CacheTrans = this.transform;
            }
            return m_CacheTrans;
        }
    }
    public uint m_UserID;
    float m_MoveSpeed = 5;
    uint m_TickInterval = 3;
    C2S_SyncMessage m_Msg = new C2S_SyncMessage();

    public bool IsLocalPlayer()
    {
        return Game.m_LocalUserID == m_UserID; 
    }

    public void Init(uint userId, Vector3 pos)
    {
        m_UserID = userId;
        CacheTrans.position = pos; 
    }

    public void Tick()
    {
        if (!IsLocalPlayer()
            || m_UserID > 1000)
        {
            return;
        }

        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        if (h != 0 || v != 0)
        {
            CacheTrans.position += new Vector3(h, 0, v).normalized * m_MoveSpeed * Time.deltaTime;  
        }

        if (Game.Instance.TickCount % m_TickInterval == 0)
        {
            m_Msg.m_Seq = BaseMessage.m_SeqIDGenerator.GetUniqueID(); 
            m_Msg.m_UserID = m_UserID;
            m_Msg.m_Pos = CacheTrans.position;
            LogManager.Error("send pos=" + m_Msg.m_Pos + " seqId=" + m_Msg.m_Seq);
            Game.Instance.Send(EMessageType.SYNC, m_Msg); 
        }
    }
}
